www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/extensions/CdcBetaTools.php

    <?php
class CdcBetaTools
{
    
    public static $version = '1.0.0 Beta 20091023';
    public static $officialUrl = 'http://www.24beta.com/';
    
    /**
     * 过滤处理超级链接标签a,去掉无用的属性,并且添加class
     * @param $html string 需要处理的包含超级链接标签的html代码
     * @return string 处理后的html代码
     */
    public static function purifyLinkTag($html)
    {
        $p = '/<a(.*?)href="(.+?)"(.*?)>(.+?)<\/a>/ism';
        $r = '<a href="$2" target="_blank">$4</a>';
        $html = preg_replace($p, $r, $html);
        return $html;
    }
    
    
    /**
     * 过滤处理img标签
     * @param $html string 需要处理的html代码
     * @param $alt string 处理替换的alt属性值
     * @param $title string 为img加上的超级链接的title属性
     * @return string 处理之后的html代码
     */
    public static function purifyImgTag($html, $title = null)
    {
        $p = '/<img .*?src="?(.+?)"?( .*?|\/|)>/ism';
        $r = sprintf('<img src="$1" class="content-pic" alt="图片:%s" />', $title);
        return preg_replace($p, $r, $html);
    }
    
    
    /**
     * 使用CHtmlPurify过滤html代码
     * @param $content string 需要过滤的html代码
     * @return string 过滤之后的 html代码
     */
    public static function purify($content, $section)
    {
        static $purifier;
        if($purifier[$section] === null) {
            $purifier[$section] = new CHtmlPurifier();
            $options = require(dirname(__FILE__) . DS . '..' . DS . 'config' . DS . 'purifier.ini.php');
            $purifier[$section]->options = $options[$section];
        }
        return $purifier[$section]->purify($content);
    }
    
    
    /**
     * 获取客户端IP地址
     * @return string 客户端IP地址
     */
    public static function getClientIp()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        return $ip;
    }
    
    
    /**
     * 过滤敏感关键字,如果replace字段替换字符串为空,则显示param('spamReplace')指定的默认字符串
     * 如果不为空,则替换为replace字段指定值
     * 支持不连续关键字过滤,如subject字段为 a{1}b{1}c,则可以过滤abc,axbc,abxc,axbxc等敏感词
     * @param $html string 需要过滤的字符串
     * @return string 过滤之后的字符串
     */
    public static function purifySpamWords($html)
    {
        // 读取所有敏感词语
        static $words;
        if ($words === null)
            $words = SpamWords::model()->getAllValidWords();
        foreach ($words as $w) {
            if (empty($w->replace)) $w->replace = param('spamReplace');
            $p = '/(.+?)\{(\d+?)\}/ism';
            $w->subject = preg_replace($p, '$1.{0,$2}', $w->subject);
            $html = preg_replace('/' . $w->subject . '/ism', '<i>' . $w->replace . '</i>', $html);
        }
        
        return $html;
    }
    
    
    public static function highlight($html)
    {
        static $hl;
        
        if ($hl === null)
            $hl = new CTextHighlighter();
        $hl->containerOptions = array('class'=>'code-block');

        $p = '/\[code language=(\w+)\](.+?)\[\/code\]/ism';
        while ($num = preg_match($p, $html, $matches)) {
            $hl->language = $matches[1] ? $matches[1] : 'php';
            $data = $hl->highlight(html_entity_decode(strip_tags($matches[2])));
            $html = preg_replace($p, $data, $html, 1);
            unset($matches);
        }
        return $html;
    }
    
    
    public static function validateCode($code, $regenerate = false)
    {
        Yii::import('application.controllers.SiteController');
	    $siteController = new SiteController('site');
	    $captcha = $siteController->createAction('captcha');
	    $sessCode = implode($captcha->getVerifyCode(), '');
	    if ($sessCode == $code) {
	        if ($regenerate) $captcha->getVerifyCode($regenerate);
	        return true;
	    }
	    else
	        return false;
    }
    

    /**
     * 返回当前程序版本
     * @return string $version
     */
    public static function getVersion()
    {
        return self::$version;
    }
    
    /**
     * 返回 Powered信息
     * @return string Powered Html
     */
    public static function getPowered()
    {
        return Chtml::link('Powered&nbsp;By&nbsp;<strong>24Beta</strong>&nbsp;' . self::getVersion(), self::$officialUrl, array('target'=>'_blank'));
    }
}